#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define TABLESIZE 50
struct {
        char *name;
        int value;
        } symtab[TABLESIZE];
int nextname = 0;

char* operators[TABLESIZE];
int nextop = 0;


int getvalue(char* s) {
        int i;
        for (i=nextname-1; i>=0; i -= 1)
                if (strcmp(s,symtab[i].name) == 0)
                        return(symtab[i].value);
	fprintf(stderr,"Name '%s' not found.\n",s);
        return (0);
}
        

void setvalue(char* s, int val) {
        int i;
        for (i=nextname-1; i>=0; i -= 1)  
                if (strcmp(s,symtab[i].name) == 0) {
                     symtab[i].value = val;
                }
}
        

int findname(char* s) {
        int i;
        for (i=nextname-1; i>=0; i -= 1)
                if (strcmp(s,symtab[i].name) == 0)
                        return(i);
        return (-1);
}

void
addname(char* s) {
        int i;
        i = findname(s);
        if (i != -1) return;
        if (nextname >= TABLESIZE) {
                fprintf(stderr,"Too many names.  Ignored '%s'.\n",s);
                return;
                }
        symtab[nextname].name = (char *) malloc(sizeof(char) * (strlen(s)+1));
        strcpy(symtab[nextname].name,s);
        nextname++;
}

void
printnames() {
        int i;
        printf("\nNAMES\n");
        for (i=0; i<nextname; i += 1)
                printf("%s\n",symtab[i].name);
}

void
addop(char* s) {
	if (nextop >= TABLESIZE) {
		fprintf(stderr,"Too many operators.  Ignored '%s'.\n",s);
		return;
		}
        operators[nextop] = (char *) malloc(sizeof(char) * (strlen(s)+1));
	strcpy(operators[nextop],s);
	nextop++;
}

void
printops() {
	int i;
	printf("\nOPERATORS\n");
	for (i=0; i<nextop; i += 1)
		printf("%s\n",operators[i]);
}
